package com.antler.smt.portal.utils;

import com.antler.smt.constant.AESConstant;
import com.antler.smt.entity.MemberEntity;
import com.antler.smt.utils.AESUtil;
import com.antler.smt.utils.Base64Util;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.utils.MD5Util;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

/**
 *  * TODO
 *  *
 *  * @author vic
 *  * @date 2018-03-28 20:49
 *  
 */
@Component
public class TokenUtil {

    private static Logger log = LogUtil.get();

    private static String key = AESConstant.SECRET_KEY;

    /**
     * 通过token获取信息
     *
     * @param token
     * @return
     */
    public static MemberEntity getMemberByToken(String token) {
        MemberEntity memberEntity = null;

        try {
            log.info("orignToken" + token);
            String originInfo = Base64Util.decryptString(token);
            log.info("token" + originInfo);
            String[] infos = originInfo.split(",");
            if (infos != null && infos.length == 2 && infos[1].equals(MD5Util.encryptString(infos[0]))) {

                    memberEntity = new MemberEntity();
                    memberEntity.setId(Long.parseLong(infos[0]));
            }

        } catch (Exception ex) {
            log.error("解析token异常！", ex);
        }

        return memberEntity;
    }

    /**
     * 通过会员信息获取token
     *
     * @param memberEntity
     * @return
     */
    public static String getTokenByMember(MemberEntity memberEntity) {


        String originInfo = memberEntity.getId() + "," + MD5Util.encryptString(memberEntity.getId().toString());
        String token = Base64Util.encryptString(originInfo);
        return token;
    }


}
